package com.cloudkinto.controller.shipprice;


import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.fbaorderoutput.FbaOrderOutputService;
import com.cloudkinto.service.fbaorderoutput.vo.FbaOrderOutputComputationalCostsReq;
import com.cloudkinto.service.fbaorderoutput.vo.FbaOrderOutputPackageAddReq;
import com.cloudkinto.service.region.JpRegionService;
import com.cloudkinto.service.shipprice.vo.ComputePriceReq;
import com.cloudkinto.service.shipprice.vo.FbaComputerPriceReq;
import com.cloudkinto.service.shipprice.vo.WarehousingPriceReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.shipprice.ShipPriceService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author ding
 * @since 2020-06-09
 */
@RestController
@Api(tags = "运费试算模块")
@RequestMapping("/api/admin/shipPrice")
public class ShipPriceController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ShipPriceController.class);

    @Autowired
    private ShipPriceService service;
    @Autowired
    private FbaOrderOutputService fbaOrderOutputService;
    @Autowired
    private JpRegionService jpRegionService;


    @GetMapping("getSizeByProductId")
    @ApiOperation(value = "获取尺寸")
    public SingleResult getSizeByProductId(Long productId){

        return SingleResult.success(service.countSameSkuSize(new double[4], productId, 1));
    }

    @GetMapping("getStateIdByPostCode")
    @ApiOperation(value = "根据右边获取stateId")
    public SingleResult getStateIdByPostCode(String postCode){
        return SingleResult.success(jpRegionService.getStateIdByPostCode(postCode));
    }


    @GetMapping("queryByRegionAndSize")
    @ApiOperation(value = "获取运费")
    public SingleResult queryByRegionAndSize(Long stateId, double weight, double size, Long storageId, Long trackId, int version){
        return SingleResult.success(service.queryByRegionAndSize(stateId, weight, size, null, null, null, 0, 0 ,storageId, trackId, version));
    }

    @PostMapping("warehousingPrice")
    @ApiOperation(value = "仓储费计算价格")
    public SingleResult warehousingPrice(@RequestBody @Validated WarehousingPriceReq req){
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.warehousingPrice(req,userId,companyId);
    }

    @UserSessionRequired
    @PostMapping("computePrice")
    @ApiOperation(value = "一件代发计算价格", notes = "计算价格")
    public SingleResult computePrice(@RequestBody @Validated ComputePriceReq req){
        Long userId = getCurrentUserId();
        //后台运费试算 公司id选择
        Long companyId = req.getCompanyId();
        return service.computePrice(req,userId,companyId);
    }

    @PostMapping("fbaComputeCosts")
    @ApiOperation(value = "FBA运费试算")
    public SingleResult fbaComputeCosts(@RequestBody @Validated FbaComputerPriceReq req) {
        //copy简单对象实现方法共用
        FbaOrderOutputComputationalCostsReq req1 = BeanConvert.INSTANCE.fbaOrderOutputComputationalCostsReq(req);
        //后台运费试算 公司id选择
        Long companyId = req.getCompanyId();
        for (FbaOrderOutputPackageAddReq fbaOrderOutputPackageAddReq : req1.getPackageAddReqList()) {
            fbaOrderOutputPackageAddReq.setPackageCount(1);
        }
        return SingleResult.success(fbaOrderOutputService.computationalCosts(req1, companyId));
    }


    @GetMapping("computeInit")
    @ApiOperation(value = "初始化", notes = "计算价格")
    public SingleResult computeInit(){
        return service.computeInit();
    }

}

